home *** CD-ROM | disk | FTP | other *** search
- /*
- * Library picture: let user select a file.
- *
- * @(#) pic_dirent.frm,v 1.6 1993/07/20 23:05:55 lasse Exp
- */
-
- Viewport view {
- Pos 0,0;
- Size 80, 23;
- }
-
- Event Key ESC { pic_leave(); }
- Event Key DOWN { fld_move(fld_down(NULL)); }
- Event Key UP { fld_move(fld_up(NULL)); }
- Event Key TAB { fld_move(fld_next(NULL)); }
- Event Key CR {
- char *p = fld_get_trimmed(NULL);
-
- if (strcmp(p, "../") == 0)
- {
- p = strrchr(dirname, '/');
- if (p != NULL)
- {
- /* Remove concatenated slashes */
- while ((p > dirname) && (p[-1] == '/')) {
- p--;
- }
-
- if (p == dirname) {
- p++; /* Never remove root slash */
- }
- *p = 0;
- }
- }
- else if (strcmp(p, "./") != 0)
- {
- strcat(dirname, "/");
- strcat(dirname, p);
-
- if (dirname[strlen(dirname) - 1] == '/') {
- dirname[strlen(dirname) - 1] = 0;
- }
-
- if (!isadir(dirname, NULL))
- {
- fld_set(field("Return"), dirname);
- pic_leave();
- return;
- }
- }
-
- fld_set(field("Dir"), dirname);
- redraw_dir();
- fld_move(field("Row0"));
- }
-
- Picture pic_dirent Viewport View
- {
- Event Draw {
- extern char *getcwd(char *buf, int size);
- if (fld_isempty(field("Dir"))) {
- getcwd(dirname, sizeof dirname);
- fld_set(field("Dir"), dirname);
- }
- redraw_dir();
- fld_move(field("Row0"));
- }
-
- Field Title {
- Pos Center, 1;
- Type Char(30);
- Forbidden;
- Value "Choose file";
- }
-
- Field Dir {
- Event Key CR { fld_move(fld_down(NULL)); }
- Event Exit {
- if (fld_ismodified(NULL)) {
- redraw_dir();
- }
- }
- Pos 12, +2; Type Char(60);
- }
-
- Field DirLabel {
- Type Char(12);
- Pos 0, +0;
- Forbidden;
- Value " Directory: ";
- }
-
- Field Row0 {Pos 0, 5; Type Char(19); Protected; }
- Field Row1 {Pos +20, +0; Type Char(19); Protected; }
- Field Row2 {Pos +20, +0; Type Char(19); Protected; }
- Field Row3 {Pos +20, +0; Type Char(19); Protected; }
-
- Field Row4 {Pos 0, +1; Type Char(19); Protected; }
- Field Row5 {Pos +20, +0; Type Char(19); Protected; }
- Field Row6 {Pos +20, +0; Type Char(19); Protected; }
- Field Row7 {Pos +20, +0; Type Char(19); Protected; }
-
- Field Row8 {Pos 0, +1; Type Char(19); Protected; }
- Field Row9 {Pos +20, +0; Type Char(19); Protected; }
- Field Row10 {Pos +20, +0; Type Char(19); Protected; }
- Field Row11 {Pos +20, +0; Type Char(19); Protected; }
-
- Field Row12 {Pos 0, +1; Type Char(19); Protected; }
- Field Row13 {Pos +20, +0; Type Char(19); Protected; }
- Field Row14 {Pos +20, +0; Type Char(19); Protected; }
- Field Row15 {Pos +20, +0; Type Char(19); Protected; }
-
- Field Row16 {Pos 0, +1; Type Char(19); Protected; }
- Field Row17 {Pos +20, +0; Type Char(19); Protected; }
- Field Row18 {Pos +20, +0; Type Char(19); Protected; }
- Field Row19 {Pos +20, +0; Type Char(19); Protected; }
-
- Field Row20 {Pos 0, +1; Type Char(19); Protected; }
- Field Row21 {Pos +20, +0; Type Char(19); Protected; }
- Field Row22 {Pos +20, +0; Type Char(19); Protected; }
- Field Row23 {Pos +20, +0; Type Char(19); Protected; }
-
- Field Row24 {Pos 0, +1; Type Char(19); Protected; }
- Field Row25 {Pos +20, +0; Type Char(19); Protected; }
- Field Row26 {Pos +20, +0; Type Char(19); Protected; }
- Field Row27 {Pos +20, +0; Type Char(19); Protected; }
-
- Field Row28 {Pos 0, +1; Type Char(19); Protected; }
- Field Row29 {Pos +20, +0; Type Char(19); Protected; }
- Field Row30 {Pos +20, +0; Type Char(19); Protected; }
- Field Row31 {Pos +20, +0; Type Char(19); Protected; }
-
- Field Row32 {Pos 0, +1; Type Char(19); Protected; }
- Field Row33 {Pos +20, +0; Type Char(19); Protected; }
- Field Row34 {Pos +20, +0; Type Char(19); Protected; }
- Field Row35 {Pos +20, +0; Type Char(19); Protected; }
-
- Field Row36 {Pos 0, +1; Type Char(19); Protected; }
- Field Row37 {Pos +20, +0; Type Char(19); Protected; }
- Field Row38 {Pos +20, +0; Type Char(19); Protected; }
- Field Row39 {Pos +20, +0; Type Char(19); Protected; }
-
- Field Row40 {Pos 0, +1; Type Char(19); Protected; }
- Field Row41 {Pos +20, +0; Type Char(19); Protected; }
- Field Row42 {Pos +20, +0; Type Char(19); Protected; }
- Field Row43 {Pos +20, +0; Type Char(19); Protected; }
-
- Field Row44 {Pos 0, +1; Type Char(19); Protected; }
- Field Row45 {Pos +20, +0; Type Char(19); Protected; }
- Field Row46 {Pos +20, +0; Type Char(19); Protected; }
- Field Row47 {Pos +20, +0; Type Char(19); Protected; }
-
- Field Row48 {Pos 0, +1; Type Char(19); Protected; }
- Field Row49 {Pos +20, +0; Type Char(19); Protected; }
- Field Row50 {Pos +20, +0; Type Char(19); Protected; }
- Field Row51 {Pos +20, +0; Type Char(19); Protected; }
-
- Field Row52 {Pos 0, +1; Type Char(19); Protected; }
- Field Row53 {Pos +20, +0; Type Char(19); Protected; }
- Field Row54 {Pos +20, +0; Type Char(19); Protected; }
- Field Row55 {Pos +20, +0; Type Char(19); Protected; }
-
- Field Help {Pos Center, +2; Type Char(20); Forbidden; }
- Field Return {Pos 0, +1; Type Char(80); Forbidden; Invisible; }
- }
-
- CCode {
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <dirent.h>
- #include <string.h>
- #include <stdlib.h>
- #include "../src/config.h"
-
- #define MAXROWS 56
-
- static int n_entries = 0;
- static int cur_entry = 0;
- static char dirname[1024];
-
- static int read_dir(char *dir);
- static void showdir(void);
- static int isadir(char *dir, char *fname);
-
- /*
- * List of entries.
- */
- static struct direlem {
- struct direlem *next;
- char *name;
- } *first = NULL;
-
- static int read_dir(char *dirname)
- {
- struct direlem *p, *next, **pp;
- struct dirent *dent;
- DIR *d;
-
- if ((d = opendir(dirname)) == NULL) return -1;
-
- /*
- * Free old list.
- */
- for (p = first; p != NULL; p = next)
- {
- next = p->next;
- if (p->name) free(p->name);
- free(p);
- }
- first = NULL;
-
- /*
- * Read the whole directory into list.
- */
- while(dent = readdir(d))
- {
- /*
- * Allocate space for entry.
- */
- if ((p = malloc(sizeof *p)) == NULL) return -1;
- /* Add 2 for tackle on trailing slash */
- if ((p->name = malloc(dent->d_reclen + 2)) == NULL) return -1;
-
- /*
- * Setup entry.
- */
- memcpy(p->name, dent->d_name, dent->d_reclen);
- p->name[dent->d_reclen] = 0;
-
- if (isadir(dirname, p->name)) {
- strcat(p->name, "/");
- }
-
- /*
- * Link entry.
- */
- for(pp = &first; *pp != NULL; pp = &(*pp)->next)
- {
- if (strcmp((*pp)->name, p->name) > 0) break;
- }
- p->next = *pp;
- *pp = p;
-
- n_entries++;
- }
- return 0;
- }
-
- static void showdir(void)
- {
- int i;
- struct direlem *p;
-
- fld_set(field("Dir"), dirname);
-
- p = first;
- for(i = 0; (i < cur_entry) && (p != NULL); i++) {
- p = p->next;
- }
-
- for(i = 0; (i < MAXROWS) && (p != NULL); i++)
- {
- fld_set(field("Row%d", i), p->name);
- p = p->next;
- }
-
- for(; i < MAXROWS; i++)
- {
- fld_set(field("Row%d", i), "");
- }
- }
-
- static void redraw_dir(void)
- {
- strcpy(dirname, fld_get_trimmed(field("Dir")));
-
- if (read_dir(dirname) != 0) {
- message("Can't open directory: %s", dirname);
- return;
- }
-
- showdir();
- }
-
- static int isadir(char *dir, char *fname)
- {
- static char buf[1024];
- struct stat stat_buf;
-
- strcpy(buf, dir);
- if (fname) {
- strcat(buf, "/");
- strcat(buf, fname);
- }
-
- stat_buf.st_mode = 0;
- stat(buf, &stat_buf);
-
- return (stat_buf.st_mode & S_IFDIR) != 0;
- }
- }
-